package com.chinanet.utils.webservice;

import org.apache.commons.lang.StringEscapeUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @Author zsf
 * @Desc Created by myseital on 2018/3/14.
 * @desc itms 模式转换webservice
 */
public class ITMSWebServiceForChangeMode {

    public String WebServiceInit(AttrsIn attrs) {
        String returnBack = "";
        try {
            URL url = new URL(
                    "http://172.20.4.13:8080/monitor/webservice/itms");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            conn.setRequestMethod("POST");
            conn.setRequestProperty("soapAction", "");
            conn.connect();

            String soap="";
                soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tz=\"http://tz_externalInterface.monitor.zjhcsoft.com/\">" +
                        "<soapenv:Header/>" +
                        "<soapenv:Body>" +
                        "<"+attrs.getFunctionName()+">" +
                        attrs.getRequestMessage() +
                        "</"+attrs.getFunctionName()+">" +
                        "</soapenv:Body>" +
                        "</soapenv:Envelope>";

            OutputStream out = conn.getOutputStream();
            out.write(soap.getBytes("UTF-8"));
            out.flush();
            int code = conn.getResponseCode();
            if (code == 200) {
                InputStream in = conn.getInputStream();
                byte b[] = new byte[10240];
                StringBuffer sb = new StringBuffer();
                int len = 0;
                while ((len = in.read(b)) != -1) {
                    String str = new String(b, 0, len, "UTF-8");
                    sb.append(str);
                }
                String ak = new String(sb.toString());
                ak = ak.replaceAll("&amp;", "");
                ak = ak.replaceAll("&lt;", "<");
                ak = ak.replaceAll("&gt;", ">");
                ak = ak.replaceAll("lt;", "<");
                ak = ak.replaceAll("gt;", ">");
                ak = ak.replaceAll("&quot;", "\"");
                ak = ak.replaceAll("&apos;", "\'");
                ak = ak.replaceAll(";", "");
                ak = ak.replaceAll("&#x", "\\\\u");
                returnBack = StringEscapeUtils.unescapeJava(ak);
                in.close();
            } else if (code == 500) {
                System.out.println("server ERROR!");
                returnBack	="server ERROR!";
            } else if (code == 504) {
                System.out.println("Gateway Timeout!");
                returnBack	="Gateway Timeout!";
            } else if (code == 404) {
                System.out.println("Not Found!");
                returnBack	="Not Found!";
            } else if (code == 408) {
                System.out.println("Request Time-Out!");
                returnBack	="Request Time-Out!";
            } else {
                System.out.println("Connection Failed!");
                returnBack	="Connection Failed!";
            }
            out.close();
            conn.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return returnBack;

    }


   /* public static void main(String[] args){
        ITMSWebServiceForChangeMode itmsWebServiceForChangeMode = new ITMSWebServiceForChangeMode();
        AttrsIn attrsIn=new AttrsIn();
        Map<String,Object> map=new HashMap<String, Object>();
        attrsIn.setFunctionName("tz:itmsBroadbandBusinessChangeMode");
        String sn = "11111111111";
        String mode = "1";
        attrsIn.setRequestMessage("<sn>"+sn+"</sn><mode>"+mode+"</mode>");
        String result = itmsWebServiceForChangeMode.WebServiceInit(attrsIn);
        result = result.substring(result.indexOf("<return>")+9,result.indexOf("</return>")-1);
        String[] arg = result.split(",");
        map.put("RetMsg",arg[0].substring(arg[0].indexOf("=")+1));
        map.put("RetCode",arg[1].substring(arg[1].indexOf("=")+1));


        System.out.println("---------");

    }*/
}
